package com.autoswf.dispatcher
{
	import com.autoswf.core.Link;
	
	import flash.events.Event;
	
	import flexunit.framework.TestCase;

	public class RemoteDispatcherTest extends TestCase
	{
		public function RemoteDispatcherTest(methodName:String=null)
		{
			super(methodName);
		}
		
		
		private var remoteDispatcher:RemoteDispatcher = new RemoteDispatcher("remoteDispatcher");
		private var sender:Link = new Link("sender");
		private var receiver:Link = new Link("receiver");
		
		private var testEvent:Event = new Event(Event.CHANGE);
		
		/* Test handler used in most tests */
		private var handler:Function = function(e:Event):void {	assertStrictlyEquals(e, testEvent);	}
		
		
		override public function setUp():void
		{
			sender.addParentLink(remoteDispatcher);
			receiver.addParentLink(remoteDispatcher);
		}
		
		
		/** Tests that a remote dispatcher will register listeners from the network. */
		public function test01():void
		{
			new RemoteListener(Event.CHANGE, new Function).sendBy(receiver);
			
			assertTrue(remoteDispatcher.hasEventListener(Event.CHANGE));
		}


		/** Tests that a remote dispatcher will remove listeners. */
		public function test02():void
		{
			new RemoteListener().add(Event.CHANGE, handler).sendBy(receiver);
			new RemoteListener().remove(Event.CHANGE, handler).sendBy(receiver);
			
			assertFalse(remoteDispatcher.hasEventListener(Event.CHANGE));
		}
				
		
		/** Tests that the remote dispatcher invokes the handler registered by a remote listener. */
		public function test03():void
		{
			new RemoteListener(Event.CHANGE, addAsync(handler, 20)).sendBy(receiver);
			
			sender.sendUp(testEvent);
		}
		
	}
}